package com.ruoyi.web.controller.comment.api;

import com.ruoyi.comment.mapper.QuestionsMapper;
import com.ruoyi.comment.service.ICommentService;
import com.ruoyi.common.annotation.Check;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.model.comment.dto.CommentDto;
import com.ruoyi.model.comment.params.CommentParams;
import com.ruoyi.model.question.Question;
import com.ruoyi.sensitive.SensitiveDTO;
import com.ruoyi.sensitive.SensitiveServiceImplV1;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author : 张怀秋
 **/
@Api(tags = "前台：评论服务")
@Slf4j
@RestController
@RequestMapping("/api/comment")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class CommentController extends BaseController {

    /**
     * 敏感词服务
     */
    private final SensitiveServiceImplV1 sensitiveServiceImplV1;

    /**
     * 评论服务
     */
    private final ICommentService commentService;

    /**
     * 提问Mapper
     */
    private final QuestionsMapper questionsMapper;


    @ApiOperation(value = "查询文章,活动,竞赛的详情信息", notes = "类型是需要带上的,分页也是需要的")
    @GetMapping("/findDetailComment/{page}/{limit}")
    public AjaxResult findDetailComment(@ApiParam("文章,活动,竞赛详情以及对应的type类型")
                                            CommentParams commentParams,
                                        @ApiParam("当前页码") @PathVariable Long page,
                                        @ApiParam("每页记录数") @PathVariable Long limit) {
        return this.commentService.queryDetailComment(commentParams, page, limit);
    }

    @ApiOperation(value = "发布评论", notes = "使用敏感词工具类屏蔽")
    @PostMapping("/send-comment")
    @Check
    public AjaxResult sendComment(@ApiParam("评论的相关信息以及层级关系") @RequestBody
                                      CommentDto commentDto) {
        if (StringUtils.isEmpty(commentDto.getContent())) {
            throw new RuntimeException("发布评论信息不能为空!");
        } else {
            SensitiveDTO sensitiveDTO = sensitiveServiceImplV1.analysisText(commentDto.
                    getContent());
            String handleContent = sensitiveDTO.getHandleContent();
            commentDto.setContent(handleContent);
            return toAjax(commentService.sendCommentContent(commentDto));
        }
    }

    @ApiOperation(value = "发布评论：提问模块", notes = "")
    @PostMapping("/post-questions")
    @Check
    public AjaxResult sendQuestionsComment(@ApiParam("提问的相关信息以及层级关系") @RequestBody CommentDto commentDto) {
        if (StringUtils.isEmpty(commentDto.getContent())) {
            throw new RuntimeException("发布发布提问信息不能为空!");
        } else {
            SensitiveDTO sensitiveDTO = sensitiveServiceImplV1.analysisText(commentDto.getContent());
            String handleContent = sensitiveDTO.getHandleContent();
            commentDto.setContent(handleContent);
            // 针对于提问回答量进行的添加
            Question question = questionsMapper.queryById(commentDto.getActivityId());
            if (question != null) {
                // todo 回答量+1
                questionsMapper.answerNum(question.getId());
            }
            return toAjax(commentService.sendCommentContent(commentDto));
        }
    }
}
